package edu.neepu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class Main {

    @Test
    public void test2() throws IOException {
        File file = new File("/home/lure/Downloads");
        if (!file.exists()) {
            file.mkdirs();
        }

        File file_son = new File(file + "/work");
        if (!file_son.exists()) {
            file_son.mkdirs();
        }

        File file_son_son = new File(file_son + "/1.txt");
        if (!file_son_son.exists()) {
            file_son_son.createNewFile();
        }
    }

    @Test
    public void test() throws IOException {
        //爬书
        Map<String, String> allBooks =  getAllBooks();

        //将数据存到下面的目录下
        File file = new File("C:\\shixun\\");
        if (!file.exists()) {
            file.mkdirs();
        }

        //测试输出
        Set<Map.Entry<String, String>> entrys = allBooks.entrySet();
        for (Map.Entry<String, String> entry: entrys) {

            String bookName = entry.getKey();   //书名
            //将书名作为目录名
            File fileBookName = new File(file + "/" + bookName);
            if (!fileBookName.exists()) {
                fileBookName.mkdirs();
            }

            String bookLink = entry.getValue();     //书链接
//            System.out.println(bookName + "=" + bookLink);

            //构造每一本书的每一个章节对应的url
            //问题:如何获取每本书中有多少章？
            int capCount = getBookOfCap(bookLink);
            for (int i = 1; i < capCount; i++) {
                //具体章节的url
                String capUrl = bookLink.substring(0, bookLink.length()-5) + "/" + i + ".html";

                Map<String, String> aBookAllCap = getBookCap(capUrl);
                Set<Map.Entry<String, String>> aBookEntrys = aBookAllCap.entrySet();
                for (Map.Entry<String, String> entry_son : aBookEntrys) {
                    String capName = entry_son.getKey();        //章节名
                    String capText = entry_son.getValue();      //文章

                    File fileCapText = new File(fileBookName + "/" + capName + ".txt");
                    if (!fileCapText.exists()) {
                        fileCapText.createNewFile();
                    }

                    FileWriter fw = new FileWriter(fileCapText);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(capText);
                    bw.close();
                    fw.close();
//                    System.out.println(capName + "=" + capText);
                }
            }
        }
    }

    //爬所有书
    public Map<String, String> getAllBooks() throws IOException {

        //用于存储书名和对应的链接
        Map<String, String> name_info = new HashMap<String, String>();

        String url = "http://www.shicimingju.com/book/";
        Document document = Jsoup.connect(url).get();   //网页源代码

        Elements all_infos = document.select("h2");

        for (Element all_info : all_infos) {
            name_info.put(all_info.select("a").text(), all_info.select("a[href]").attr("abs:href"));
        }

        return name_info;
    }

    //用于获取章节名和文本信息
    public Map<String, String> getBookCap(String url) throws IOException {

        //用于存储章节名和文本信息
        Map<String, String> name_info = new HashMap<String, String>();

        Document document = Jsoup.connect(url).get();   //文本网页源代码
        Elements all_infos = document.select(".layui-col-md8");

        for (Element all_info : all_infos) {
            name_info.put(all_info.select("h1").text(), all_info.select("p").text());
        }

        return name_info;
    }

    //获取每个本书中有多少个章节
    public int getBookOfCap (String url) throws IOException {

        int count = 0;
        Document document = Jsoup.connect(url).get();
        Elements test = document.select(".book-mulu").select("a");

        for (Element element : test) {
            count++;
//            System.out.println(element.text());
        }
        return count;
    }
}
